package com.cy.store.service.impl;

import com.cy.store.entity.Product;
import com.cy.store.mapper.ProductMapper;
import com.cy.store.service.IProductService;
import com.cy.store.service.ex.ProductNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class ProductServiceImpl implements IProductService {

    @Autowired
    private ProductMapper productMapper;

    /**
     * 使用id查询商品信息
     *
     * @param id
     * @return
     */
    @Override
    public Product findById(Integer id) {
        Product product = productMapper.findById(id);
        if (product == null) {
            throw new ProductNotFoundException("未查询到相关数据");
        }

        // 将查询结果中的部分属性设置为null
        product.setPriority(null);
        product.setCreatedUser(null);
        product.setCreatedTime(null);
        product.setModifiedUser(null);
        product.setModifiedTime(null);

        return product;
    }

    /**
     * 显示最新的产品
     *
     * @return
     */
    @Override
    public List<Product> findNewList() {
        List<Product> list = productMapper.findNewList();
        list = list.stream().map((item) -> {
            item.setPriority(null);
            item.setCreatedTime(null);
            item.setCreatedUser(null);
            item.setModifiedTime(null);
            item.setModifiedUser(null);
            return item;
        }).collect(Collectors.toList());
        return list;
    }

    /**
     * 显示热销产品
     *
     * @return
     */
    @Override
    public List<Product> findHostList() {

        List<Product> list = productMapper.findHotList();
        list = list.stream().map((item) -> {
            item.setPriority(null);
            item.setCreatedTime(null);
            item.setCreatedUser(null);
            item.setModifiedTime(null);
            item.setModifiedUser(null);
            return item;
        }).collect(Collectors.toList());
        return list;
    }
}
